<?php
namespace Api\Controller;

use Think\Controller;

class ReviewController extends Controller
{

    protected $review_model;
    protected $review_image_model;

    public function _initialize()
    {
        $this->review_model = D("Common/Review");
        $this->review_image_model = D("Common/ReviewImage");

    }

    /**
     * 根据菜谱id获取所有评论列表（评论和回复）
     */
    public function getReviewList()
    {
        $cookId = I('cook_id');
        $sql = "SELECT cr.*,cou.`avatar` head_img,cou.`user_nicename` username FROM cmf_review cr  
            LEFT JOIN cmf_users cou ON cou.id = cr.uid
            WHERE cr.review_id = 0 AND cr.cook_id = $cookId

                ORDER BY cr.create_time DESC";
        $reviewss = $this->review_model->query($sql);
        $reviews = [];
        foreach ($reviewss as $vo) {
            $id = $vo['id'];
            $sql = "SELECT cr.*,cou.`avatar` head_img,cou.`user_nicename` username FROM cmf_review cr  
                 LEFT JOIN cmf_users cou ON cou.id = cr.uid
                WHERE cr.review_id = $id AND cr.cook_id = $cookId
                                ORDER BY cr.create_time DESC";
            $replyss= $this->review_model->query($sql);
            $replys = [];
            foreach($replyss as $co){
                $replyId = $co['id'];
                $iamges = $this->review_image_model->where(array('review_id' => $replyId))->select();
                $co['photos'] = $iamges;
                $replys[] = $co;
            }
            $vo['replys'] = $replys;
            $photos = $this->review_image_model->where(array('review_id' => $id))->select();
            $vo['photos'] = $photos;
            $reviews[] = $vo;
        }


        $this->returnok($reviews, 'ok');
    }

    // 用户发起评论添加提交
    public function addReview()
    {
        $ouId = I('ouId');
//        $this->returnok($ouId, '发起评论成功');
        //根据第三方用户ouId查询本站用户id
        $oauth_user_model=M('OauthUser');
        $uid=$oauth_user_model->where("id = $ouId")->getField('uid');
        $cookId = I('cookId');
        $reviewId = 0;
        $content = I('content');
        $photos = I('photos');
        $photos = str_replace("'", '"', $photos);
        $photos = json_decode($photos);
        $data = array(
            'cook_id' => $cookId,
            'user_id' => $ouId,
            'uid' => $uid,
            'review_id' => $reviewId,
            'content' => $content,
            'create_time' => date("Y-m-d H:i:s")
        );
        $id = $this->review_model->add($data);
        // 发表评论图片
        foreach ($photos as $photo) {
            $pd = array(
                'review_id' => $id,
                'file_name' => $photo->savename,
                'file_path' => $photo->savepath,
                'create_time' => date("Y-m-d H:i:s")
            );
            $this->review_image_model->add($pd);
        }
        $this->returnok('', '发起评论成功');
    }

    // 用户回复评论添加提交
    public function addReply()
    {
        $ouId = I('ouId');
        //根据第三方用户ouId查询本站用户id
        $oauth_user_model=M('OauthUser');
        $uid=$oauth_user_model->where("id=$ouId")->getField('uid');
        $cookId = I('cookId');
        $reviewId = I('reviewId');
        $content = I('content');
        $photos = I('photos');
        $photos = str_replace("'", '"', $photos);
        $photos = json_decode($photos);
        $data = array(
            'cook_id' => $cookId,
            'user_id' => $ouId,
            'uid' => $uid,
            'review_id' => $reviewId,
            'content' => $content,
            'create_time' => date("Y-m-d H:i:s")
        );
        $id = $this->review_model->add($data);
        // 发表评论图片
        foreach ($photos as $photo) {
            $pd = array(
                'review_id' => $id,
                'file_name' => $photo->savename,
                'file_path' => $photo->savepath,
                'create_time' => date("Y-m-d H:i:s")
            );
            $this->review_image_model->add($pd);
        }
        $this->returnok('', '回复评论成功');
    }
}